package edu.ncsu.csc.ip.client.run;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import edu.ncsu.csc.ip.client.PeerClient;
import edu.ncsu.csc.ip.client.protocol.ProtocolClientImpl;
import edu.ncsu.csc.ip.common.Log;

public class RunTask2 {

	public static void main(String[] args) throws IOException {

		boolean batch1 = false;
		boolean batch2 = false;
		boolean batch3 = false;

		PeerClient peer0 = null;
		PeerClient peer1 = null;
		PeerClient peer2 = null;
		PeerClient peer3 = null;
		PeerClient peer4 = null;
		PeerClient peer5 = null;

		Task2Thread peer0Thread = null;
		Task2Thread peer1Thread = null;
		Task2Thread peer2Thread = null;
		Task2Thread peer3Thread = null;
		Task2Thread peer4Thread = null;
		Task2Thread peer5Thread = null;

		int[][] rfcNumbers = {
				{ 6969, 6970, 6971, 6972, 6973, 6974, 6975, 6976, 6977, 6978,
						6979, 6980, 6981, 6982, 6983, 6984, 6985, 6986, 6987,
						6989, 6990, 6991, 6992, 6993, 6994, 6996, 6997, 6998,
						7001, 7002, 7003, 7004, 7005, 7007, 7008, 7009, 7010,
						7011, 7012, 7013, 7014, 7015, 7017, 7018, 7020, 7021,
						7022, 7025, 7026, 7028 },
				{ 6958, 6959, 6960, 6961, 6962, 6963, 6964, 6965, 6967, 6968,
						6979, 6980, 6981, 6982, 6983, 6984, 6985, 6986, 6987,
						6989, 6990, 6991, 6992, 6993, 6994, 6996, 6997, 6998,
						7001, 7002, 7003, 7004, 7005, 7007, 7008, 7009, 7010,
						7011, 7012, 7013, 7014, 7015, 7017, 7018, 7020, 7021,
						7022, 7025, 7026, 7028 },
				{ 6958, 6959, 6960, 6961, 6962, 6963, 6964, 6965, 6967, 6968,
						6969, 6970, 6971, 6972, 6973, 6974, 6975, 6976, 6977,
						6978, 6990, 6991, 6992, 6993, 6994, 6996, 6997, 6998,
						7001, 7002, 7003, 7004, 7005, 7007, 7008, 7009, 7010,
						7011, 7012, 7013, 7014, 7015, 7017, 7018, 7020, 7021,
						7022, 7025, 7026, 7028 },
				{ 6958, 6959, 6960, 6961, 6962, 6963, 6964, 6965, 6967, 6968,
						6969, 6970, 6971, 6972, 6973, 6974, 6975, 6976, 6977,
						6978, 6979, 6980, 6981, 6982, 6983, 6984, 6985, 6986,
						6987, 6989, 7003, 7004, 7005, 7007, 7008, 7009, 7010,
						7011, 7012, 7013, 7014, 7015, 7017, 7018, 7020, 7021,
						7022, 7025, 7026, 7028 },
				{ 6958, 6959, 6960, 6961, 6962, 6963, 6964, 6965, 6967, 6968,
						6969, 6970, 6971, 6972, 6973, 6974, 6975, 6976, 6977,
						6978, 6979, 6980, 6981, 6982, 6983, 6984, 6985, 6986,
						6987, 6989, 6990, 6991, 6992, 6993, 6994, 6996, 6997,
						6998, 7001, 7002, 7014, 7015, 7017, 7018, 7020, 7021,
						7022, 7025, 7026, 7028 },
				{ 6958, 6959, 6960, 6961, 6962, 6963, 6964, 6965, 6967, 6968,
						6969, 6970, 6971, 6972, 6973, 6974, 6975, 6976, 6977,
						6978, 6979, 6980, 6981, 6982, 6983, 6984, 6985, 6986,
						6987, 6989, 6990, 6991, 6992, 6993, 6994, 6996, 6997,
						6998, 7001, 7002, 7003, 7004, 7005, 7007, 7008, 7009,
						7010, 7011, 7012, 7013 } };

		try {
			String[] arg0 = args[0].split(":");
			String rsAddress = arg0[0];
			int rsPort = Integer.parseInt(arg0[1].trim());

			if (args.length > 1) {
				if (args[1].indexOf("1") != -1) {
					Log.println("Running batch 1");
					batch1 = true;
				}
				if (args[1].indexOf("2") != -1) {
					Log.println("Running batch 2");
					batch2 = true;
				}
				if (args[1].indexOf("3") != -1) {
					Log.println("Running batch 3");
					batch3 = true;
				}
			} else {
				batch1 = true;
				batch2 = true;
				batch3 = true;
				Log.println("Running All batches");
			}

			if (batch1) {
				peer0 = new PeerClient(rsAddress, rsPort, 65450, "./rfc_0/");
				peer1 = new PeerClient(rsAddress, rsPort, 65451, "./rfc_1/");
			}
			if (batch2) {
				peer2 = new PeerClient(rsAddress, rsPort, 65452, "./rfc_2/");
				peer3 = new PeerClient(rsAddress, rsPort, 65453, "./rfc_3/");
			}
			if (batch3) {
				peer4 = new PeerClient(rsAddress, rsPort, 65454, "./rfc_4/");
				peer5 = new PeerClient(rsAddress, rsPort, 65455, "./rfc_5/");
			}

			// 1
			if (batch1) {
				peer0.runCommand(ProtocolClientImpl.REGISTER);
				peer1.runCommand(ProtocolClientImpl.REGISTER);
			}
			if (batch2) {
				peer2.runCommand(ProtocolClientImpl.REGISTER);
				peer3.runCommand(ProtocolClientImpl.REGISTER);
			}
			if (batch3) {
				peer4.runCommand(ProtocolClientImpl.REGISTER);
				peer5.runCommand(ProtocolClientImpl.REGISTER);
			}
			
			
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(
					System.in));
			Log.println("Press Enter To Continue ...");
			 stdIn.readLine();
			// 2
			if (batch1) {
				peer0.runCommand(ProtocolClientImpl.PQUERY);
				peer1.runCommand(ProtocolClientImpl.PQUERY);
			}
			if (batch2) {
				peer2.runCommand(ProtocolClientImpl.PQUERY);
				peer3.runCommand(ProtocolClientImpl.PQUERY);
			}
			if (batch3) {
				peer4.runCommand(ProtocolClientImpl.PQUERY);
				peer5.runCommand(ProtocolClientImpl.PQUERY);
			}
			// 3
			if (batch1) {
				peer0.runCommand(ProtocolClientImpl.RFCQUERY);
				peer1.runCommand(ProtocolClientImpl.RFCQUERY);
			}
			if (batch2) {
				peer2.runCommand(ProtocolClientImpl.RFCQUERY);
				peer3.runCommand(ProtocolClientImpl.RFCQUERY);
			}
			if (batch3) {
				peer4.runCommand(ProtocolClientImpl.RFCQUERY);
				peer5.runCommand(ProtocolClientImpl.RFCQUERY);
			}
			if (batch1) {
				peer0Thread = new Task2Thread("Peer0", peer0, rfcNumbers[0]);
				peer1Thread = new Task2Thread("Peer1", peer1, rfcNumbers[1]);
			}
			if (batch2) {
				peer2Thread = new Task2Thread("Peer2", peer2, rfcNumbers[2]);
				peer3Thread = new Task2Thread("Peer3", peer3, rfcNumbers[3]);
			}
			if (batch3) {
				peer4Thread = new Task2Thread("Peer4", peer4, rfcNumbers[4]);
				peer5Thread = new Task2Thread("Peer5", peer5, rfcNumbers[5]);
			}
			if (batch1) {
				peer0Thread.start();
				peer1Thread.start();
			}
			if (batch2) {
				peer2Thread.start();
				peer3Thread.start();
			}
			if (batch3) {
				peer4Thread.start();
				peer5Thread.start();
			}
		} catch (Exception e) {
			Log.err("Usage:\n	RunTask2 <RS IP>:<RS Port> [batch#]");
			Log.err("Example:\n	RunTask2 10.8.11.52:65423 13");
			Log.err("        	RunTask2 10.8.11.52:65423 132");
			Log.err("        	RunTask2 10.8.11.52:65423");
			System.exit(1);
		}
	}
}
